package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.AttrMapper;
import com.ktmall.ktmalluijava.pojo.Attr;
import com.ktmall.ktmalluijava.service.AttrService;
import com.ktmall.ktmalluijava.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/15 下午7:53
 */
@Service
public class AttrServiceImpl implements AttrService {
    @Resource
    private AttrMapper attrMapper;

    /**
     * 获取分页对象
     *
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Attr> getPage(String name, Integer pageNo, Integer pageSize) {
        Page<Attr> page = new Page<>();
        page.setPageSize(pageSize);
        page.setTotalCount(attrMapper.count(name));
        page.setPageNo(pageNo);
        int offset = (page.getPageNo()-1)*page.getPageSize();
        page.setData(attrMapper.select(name,offset,pageSize));
        return page;
    }

    /**
     * 添加
     *
     * @param attr
     * @return
     */
    @Override
    public int add(Attr attr) {
        return attrMapper.add(attr);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public Attr detail(Integer id) {
        return attrMapper.detail(id);
    }

    /**
     * 修改
     *
     * @param attr
     * @return
     */
    @Override
    public int update(Attr attr) {
        return attrMapper.update(attr);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer... id) {
        if(id.length==1){
            return attrMapper.delete(id[0]);
        }
       int count = 0;
        for (Integer integer : id) {
            count += attrMapper.delete(integer);
        }
        return count;
    }

    /**
     * 获取所有的规格属性
     *
     * @return
     */
    @Override
    public List<Attr> getAll() {
        return attrMapper.getAll();
    }
}
